﻿Public Partial Class ViewRoles
    Inherits BaseUserControl

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Rebind()
        End If
    End Sub

    Protected Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        If TypeOf sender Is Control AndAlso CType(sender, Control).ID = "btnDelete" Then
            Dim roleName As String = CType(sender, ImageButton).CommandArgument
            Try
                If Roles.GetUsersInRole(roleName).Length = 0 Then
                    Roles.DeleteRole(roleName)
                    Me.Prompt("Role " & roleName & " deleted successfuly!", UserControlPromptTypes.Successful)
                    Me.NotifyUpdate(sender, e)
                Else
                    Me.Prompt("Role " & roleName & " couldn't be deleted.<br />It has one or more user(s)!", UserControlPromptTypes.Alert)
                End If
            Catch ex As Exception
                Me.RaiseError(ex)
            End Try
        End If
    End Sub

    Private Sub Page_OnUpdate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.OnUpdate
        Rebind()
    End Sub

    Public Overrides Sub Rebind()
        Try
            rptRoles.DataSource = Roles.GetAllRoles
            rptRoles.DataBind()
        Catch ex As Exception
            Me.RaiseError(ex)
        End Try
    End Sub

    Protected Shared Function GetUsersCount(ByVal roleName As String) As Integer
        Return Roles.GetUsersInRole(roleName).Length
    End Function

End Class